package com.dabay.web.service;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.dabay.web.Model.Sbbt;
import com.dabay.web.util.Helper;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class XmlParserService {
	private Helper helper = new Helper();
	 public static  String xml2JSON(String xmlStr) {  
	        JSONObject obj = new JSONObject();  
	        try {  
	            InputStream is = new ByteArrayInputStream(xmlStr.getBytes("utf-8"));  
	            SAXBuilder sb = new SAXBuilder();  
	            Document doc = sb.build(is);  
	            Element root = doc.getRootElement();  
	            obj.put(root.getName(), iterateElement(root));  
	            return obj.toString();  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	            return null;  
	        }  
	    }  
	 
	 public static Map  iterateElement(Element element) {  
	        List jiedian = element.getChildren();  
	        Element et = null;  
	        Map obj = new HashMap();  
	        List list = null;  
	        for (int i = 0; i < jiedian.size(); i++) {  
	            list = new LinkedList();  
	            et = (Element) jiedian.get(i);  
	            if (et.getTextTrim().equals("")) {  
	                if (et.getChildren().size() == 0)  
	                    continue;  
	                if (obj.containsKey(et.getName())) {  
	                    list = (List) obj.get(et.getName());  
	                }  
	                list.add(iterateElement(et));  
	                obj.put(et.getName(), list);  
	            } else {  
	                if (obj.containsKey(et.getName())) {  
	                    list = (List) obj.get(et.getName());  
	                }  
	                list.add(et.getTextTrim());  
	                obj.put(et.getName(), list);  
	            }  
	        }  
	        return obj;  
	    } 
	 //返回的消息头处理
	 public   Map headResponseDeal(String xmlStr) {  
		 	Map resultMap =  new HashMap();
		 	resultMap.put("FHM", "");
		 	resultMap.put("FHSM", "");
	        JSONObject obj = new JSONObject();  
	        try {  
	        	JSONObject jsonObject = JSONObject.fromObject(xmlStr);
	    		String  responeHeadData = jsonObject.getJSONObject("TZM").get("TZH").toString();
	    		responeHeadData = responeHeadData.substring(1, responeHeadData.length()-1);
	    		String resultHeadStr = responeHeadData.replaceAll("\\[", "").replaceAll("\\]", "");
	    		JSONObject headObject = JSONObject.fromObject(resultHeadStr); 
	    		resultMap.put("FHM", helper.parseObjToStr(headObject.get("FHM")));
			 	resultMap.put("FHSM",helper.parseObjToStr(headObject.get("FHSM")));
	            return resultMap;  
	        } catch (Exception e) {  
	        	e.printStackTrace();
	        	resultMap.put("FHM", "ERROR");
			 	resultMap.put("FHSM", "消息头报文解析错误"+e.fillInStackTrace());
	            return resultMap;  
	        }  
	    }  
	 //返回的数据处理
	 public   JSONArray getDataArray(String xmlStr) {  
			JSONObject jsonObject = JSONObject.fromObject(xmlStr);
			String  responseBodyData = jsonObject.getJSONObject("TZM").get("TZB").toString();
			responseBodyData = responseBodyData.substring(1, responseBodyData.length()-1);
			JSONObject dataJson = JSONObject.fromObject(responseBodyData);
			JSONArray dataArray = null;
			if(helper.parseObjToStr(dataJson.get("SJ")).length()>10){//其实就是判断返回没查到数据 数据为空的情况
				dataArray = dataJson.getJSONArray("SJ");
			}
			return dataArray;
	    }  
	 public static void main(String[] args) {
		String aa = "<TZM>  <TZH>    <BBH>1.0</BBH>    <FWM>CY_SBBT_CX</FWM>    <SID>*</SID>    <FHM>0</FHM>    <FHSM></FHSM>  </TZH>  <TZB>    <SJ>      <BBC017>A00000EL1</BBC017>      <AAB003>14</AAB003>      <R00046>11</R00046>      <AAE006>000001SO</AAE006>      <R00048>20120412</R00048>      <AAC002>330102197002231539</AAC002>      <AAC003>张晶</AAC003>      <R0004B>大中专毕业生</R0004B>      <R0004H>201509</R0004H>      <R0004J>201604</R0004J>      <AAE004>达达达大大大</AAE004>      <SHZT>31</SHZT>      <SHSM>恭喜您，预受理成功</SHSM>      <SHRQ>20160504</SHRQ>      <SHR>余姚开发区发区</SHR>      <R0005P>1</R0005P>      <R0005Q>1</R0005Q>      <R0005R>1</R0005R>      <R0004D></R0004D>    </SJ>    <SJ>      <BBC017>A00000B8J</BBC017>      <AAB003>123456</AAB003>      <R00046>11</R00046>      <AAE006>000001SO</AAE006>      <R00048>20120412</R00048>      <AAC002>330102197002231539</AAC002>      <AAC003>张晶</AAC003>      <R0004B>大中专毕业生</R0004B>      <R0004H>201509</R0004H>      <R0004J>201603</R0004J>      <AAE004>建达建达</AAE004>      <SHZT>31</SHZT>      <SHSM>恭喜您，预受理成功</SHSM>      <SHRQ>20160425</SHRQ>      <SHR>余姚开发区发区</SHR>      <R0005P>1</R0005P>      <R0005Q>1</R0005Q>      <R0005R>1</R0005R>      <R0004D></R0004D>    </SJ>  </TZB></TZM>";
		String aabb = xml2JSON(aa);
		//System.out.println(aabb);
		JSONObject jsonObject = JSONObject.fromObject(aabb);
		String  responseBodyData = jsonObject.getJSONObject("TZM").get("TZB").toString();
		responseBodyData = responseBodyData.substring(1, responseBodyData.length()-1);
		JSONObject dataJson = JSONObject.fromObject(responseBodyData);
		JSONArray dataArray = null;
		dataArray =dataJson.getJSONArray("SJ");
		 if(dataArray!=null && dataArray.size()>0){
			 for(int i=0;i< dataArray.size();i++){
				 String resultStr = dataArray.get(i).toString().replaceAll("\\[", "").replaceAll("\\]", "");
				 JSONObject object = JSONObject.fromObject(resultStr);;  
				 Sbbt sbbt = (Sbbt)JSONObject.toBean(object,Sbbt.class);
			 }
	    }
	 }
}

